import Vue from 'vue'
import VueRouter from 'vue-router'

import '/src/components/css/resources/css/axure_rp_page.css'
import '/src/components/css/data/styles.css'
import '/src/components/css/files/home/styles.css'
import '/src/components/css/files/login/styles.css'
import '/src/components/css/files/product/styles.css'
import '/src/components/css/files/center/styles.css'
import '/src/components/css/files/pim/styles.css'
import CommonView from "@/views/CommonView.vue";
import CartView from "@/views/CartView.vue";
import FlowerInfoView from "@/views/FlowerInfoView.vue";
import checkOutView from "@/views/CheckOutView.vue";
import OrderItemView from "@/views/OrderItemView.vue";
import PersonView from "@/views/Person/PersonView.vue";
import OrderView from "@/views/Person/OrderView.vue";
import CollectionView from "@/views/Person/CollectionView.vue";
import HomeView from "@/views/HomeView.vue";
import LoginView from "@/views/LoginView.vue";
import RegisterView from "@/views/RegisterView.vue";
import ProductView from "@/views/ProductView.vue";
import CenterView from "@/views/Person/CenterView.vue";
import PIMView from "@/views/Person/PIMView.vue";
import SiteView from "@/views/Person/SiteView.vue";
import EvaluateView from "@/views/Person/EvaluateView.vue";
import {Message} from "element-ui";
import axios from "axios";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'CommonView',
        component: CommonView,
        children: [
            {path: '/', name: 'HomeView', component: HomeView},
            {path: '/login', name: 'LoginView', component: LoginView},
            {path: '/register', name: 'RegisterView', component: RegisterView},
            {path: '/product', name: 'ProductView', component: ProductView},

            {path: '/flower', name: 'FlowerInfoView', component: FlowerInfoView,},
            {path: '/cart', name: 'CartView', component: CartView,},
            {path: '/checkOut', name: 'CheckOutView', component: checkOutView,},
            {path: '/orderItem', name: 'OrderItemView', component: OrderItemView,},
            {path: '/person', name: 'PersonView', component: PersonView,
                children: [
                    {path: '/center', name: 'CenterView', component: CenterView},
                    {path: '/pim', name: 'PIMView', component: PIMView},
                    {path: '/site', name: 'SiteView', component: SiteView},
                    {path: '/evaluate', name: 'EvaluateView', component: EvaluateView},

                    {path:'/order', name: 'OrderView', component: OrderView,},
                    {path:'/collection', name: 'CollectionView', component: CollectionView,}
                ]
            }
        ]
    },
]
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
    routes
})

// 添加路守卫
router.beforeEach(async (to, from, next) => {
    // 按官网提供的示例，做修改
    let token = sessionStorage.getItem("token");
    // console.log(token)
    // console.log("上一个视图", from)
    // console.log("要访问的视图名：" + to.name)
    if (token == null && to.name != 'HomeView'&& to.name != 'LoginView'&& to.name != 'RegisterView') {
        // console.log("---- 前端守卫，拦截没有登录 ----")
        // 将用户重定向到登录页面
        sessionStorage.clear();
        Message({
            type: "error",
            message: "对不起，请先登录",
            duration: 1000
        });
        next({ name: 'LoginView' });
    }
    next();
});

// ---------- 设置请求拦截器：让后面的每次请求都带上token ------------
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    // 1: 得到本地缓存的token
    let token = sessionStorage.getItem("token");
    // console.log("请求拦截器里的token:" + token);

    // 2：把token设置到请求头中
    if (token) {
        // 如果token是有值的，就设置进头中

        config.headers['authentication'] = token;
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

export default router

axios.interceptors.response.use(res => {
    if (res.data.code === 501) {
        // 跳转到登录页面
        sessionStorage.clear();
        router.push({ name: 'LoginView' });
        window.location.reload();
        Message({
            type: "error",
            message: "抱歉，登录时长到期，请重新登录！",
            duration: 10000
        })
    }
    return res;
});